package com.muyu.product.service.impl;

import com.muyu.common.core.domain.R;
import com.muyu.product.domain.pojo.ProductClassifyPO;
import com.muyu.product.domain.vo.ProductClassifyVo;
import com.muyu.product.mapper.ProductClassifyMapper;
import com.muyu.product.service.ProductClassifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ProductClassifyServiceImpl
 * @Description 描述
 * @Author ShiYu.Cui
 * @Date 2024/3/29 16:25
 */
@Service
@Slf4j
public class ProductClassifyServiceImpl implements ProductClassifyService {
    @Autowired
    private ProductClassifyMapper productClassifyMapper;

    @Override
    public List<ProductClassifyVo> getAll() {
       List<ProductClassifyPO> classifyPos= productClassifyMapper.getAll();
        Map<Integer, List<ProductClassifyVo>> map = new HashMap<>();

        //将ProductClassifyPO转换为ProductClassifyVo
        List<ProductClassifyVo> classifyVos= classifyPos.stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());

        //构建树形结构
        for (ProductClassifyVo classifyVo : classifyVos) {
            if(classifyVo.getParentId()!=null && classifyVo.getParentId()!=0){
                map.computeIfAbsent(classifyVo.getParentId(),k -> new ArrayList<>()).add(classifyVo);
            }
        }

        //递归设置子集
        classifyVos.forEach(classifyVo -> setChildren(classifyVo,map));

        //返回根节点分类
        return classifyVos.stream()
                .filter(classifyVo -> classifyVo.getParentId() == 0)
                .collect(Collectors.toList());
    }

    @Override
    public R<List<ProductClassifyPO>> findBy(Integer parentId) {
        List<ProductClassifyPO> list=productClassifyMapper.findBy(parentId);
        return R.ok(list);
    }

    @Override
    public void addClassify(ProductClassifyPO productClassifyPO) {
        productClassifyMapper.addClassify(productClassifyPO);
    }

    @Override
    public void updateClassify(ProductClassifyPO productClassifyPO) {
        productClassifyMapper.updateClassify(productClassifyPO);
    }

    @Override
    public void delClassify(Integer id) {
        productClassifyMapper.delClassify(id);
    }

    private ProductClassifyVo convertToVo(ProductClassifyPO classifyPO) {
        ProductClassifyVo classifyVo = new ProductClassifyVo();
        classifyVo.setId(classifyPO.getId());
        classifyVo.setClassifyName(classifyPO.getClassifyName());
        classifyVo.setParentId(classifyPO.getParentId());
        return classifyVo;
    }

    private void setChildren(ProductClassifyVo classifyVo, Map<Integer, List<ProductClassifyVo>> map) {
        List<ProductClassifyVo> children = map.get(classifyVo.getId());
        if(children!=null){
            classifyVo.setChildren(children);
            for (ProductClassifyVo child : children) {
                setChildren(child,map);
            }
        }
    }


}
